home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 942 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  6.2 KB

  1. From: Philippe Verdy <100105.3120@compuserve.com>
  2. Message-ID: <4jsk6r$t2m@dub-news-svc-3.compuserve.com>
  3. X-Original-Date: 3 Apr 1996 01:27:55 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 03 Apr 96 03:13:06 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Interest of static constructors, semantics, examples
  9. Organization: CompuServe Incorporated
  10. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  11.     iQBFAgUBMWHs1OEDnX0m9pzZAQHAAQF+KP5XtLs1w1u+Tx4UktSGHcer2KAn9rlT
  12.     uCzl5RcbNKz5blP/xBe8blqQR+s+DYgy
  13.     =mVRw
  14.  
  15. I should first state the semantic and rules of static
  16. constructor before proceeding further with static inheritance
  17.  
  18. - If a class defines a static constructor, its static members
  19.   must not be initialized. Only the whole class can be
  20.   initialized with the following syntax:
  21.   static class X;
  22.     // uses the static default constructor
  23.   or:
  24.   static class X(parameters);
  25.     // uses a parameterized static constructor.
  26.     // Optionally supported, because it adds syntax conflicts
  27.   or:
  28.   static (class X(parameters));
  29.     // to solve the ambiguities
  30.  
  31. - A static constructor initialize its static members the
  32.   same way an instance constructor calls the constructors
  33.   for each of their non-static members:
  34.   static X::X()
  35.     : static_member1(value1)
  36.     , static_member2(value2)
  37.   {
  38.     // additional code
  39.   };
  40.  
  41. - All static members not described in this list must have
  42.   a default constructor defined, which will be implicitly
  43.   called in declaration order after each explicit call of
  44.   static member constructors.
  45.  
  46. - A static constructor can call the standard constructor
  47.   of a class from which it derives statically:
  48.   class Y { ... };
  49.   class X : static Y
  50.   {
  51.     static X()
  52.       : Y() // calls the standard instance constructor of Y
  53.       , classmember1(value1)
  54.     { ... }
  55.     X() {...} // standard default constructor for instances
  56.   };
  57.  
  58. - If a static base class Y (we will call it a metaclass) is
  59.   not initialized by calling one of its constructors from the
  60.   static constructor of the class, the metaclass Y must have
  61.   a default constructor which will be called implicitly
  62.   in the order of declaration of the metaclasses, but after
  63.   the other explicit metaclass constructions
  64.  
  65. - The static members and metaclasses destructors will be
  66.   implicitly called in the reverse order of the calls of
  67.   constructors.
  68.  
  69. - There should only be one static constructor for any class,
  70.   because this class construction will occur only once for
  71.   the whole application.
  72.  
  73. - Classes may have static base classes, called metaclasses,
  74.   in addition to their static members. This means that the
  75.   class inherits statically of a unique instance of its
  76.   metaclass. The methods which apply on instances of these
  77.   base classes will apply to the class itself, even without
  78.   specifying any of its instances.
  79.   The access rules (public, protected, private) for these
  80.   inherited properties (attributes and methods) are the same
  81.   as for standard (non-static) base classes properties.
  82.   Without its additional methods and members, the class
  83.   can be seen as a static variable, constructed with the
  84.   code implemented in the static constructor.
  85.  
  86. - Multiple static inheritance is possible. In fact, its like
  87.   if we built an anonymous class which inherits non statically
  88.   from the same bases, and used it to build a classic (but
  89.   anonymous) instance, which properties would be fully
  90.   accessible within our class. The rules for the construction
  91.   of this instance are the same as the rule of constuction
  92.   of a static instance of a standard class which has multiple
  93.   inheritance.
  94.  
  95. - static virtual inheritance is implied for the same reasons.
  96.   This is to solve the conflicts which come from multiple
  97.   inheritance, the same way we did for non-static inheritance.
  98.  
  99. - static functions can be made virtual. This will most often
  100.   occur if one of the static base classes define non-static
  101.   virtual methods, so that we can override them within our
  102.   class, while being able to call them directly from the
  103.   base class.
  104.   
  105. - There is no problem of multiple base class constructions,
  106.   because we don't call directly the static base class
  107.   constructor. Instead, the compiler generates at run-time
  108.   a virtual table by using a flag in each class vtable which
  109.   will also be used to chain the classes constructed, so that
  110.   destructors are called in the appropriate order.
  111.  
  112. Now here are the interest of static constructors:
  113.  
  114. - You can create a metaclass/class hierarchy which fixes the
  115.   order of construction of each classes.
  116. - You can use multiple inheritance of containers to create
  117.   custom class registration with much more power than RTTI.
  118. - Using this feature, you avoid most of templates, and
  119.   still have more compact code, while allowing for dynamic
  120.   registration of classes at run-time (for example when
  121.   loading additional classes from a dynamically loaded DLL
  122.   or driver). We can use virtual static function pointers as
  123.   arguments to the constructors of static baseclasses.
  124.  
  125. Example:
  126.  
  127. class RegisterClass {
  128. public:
  129.   class MyClass {
  130.   public:
  131.     MyClass(RegisterClass *cl, const char *name) {
  132.       C = cl;
  133.       N = nm;
  134.     }
  135.     RegisterClass *C;
  136.     char          *N;
  137.   }
  138.  
  139.   RegisterClass(char *name)
  140.   {
  141.     reg.insert(new MyClass(this, name));
  142.   }
  143.  
  144. private:
  145.   static Array<MyClass * const> reg;
  146. }
  147.  
  148. class Y : virtual static RegisterClass
  149. {
  150.   static Y()
  151.   : RegisterClass("Y")
  152.   {
  153.     // some code to do with constructed static members of Y
  154.     // make Y usable to create instances
  155.   };
  156.   ...
  157. }
  158.  
  159. class X : Y, virtual static RegisterClass
  160. {
  161.   static X()
  162.   : Y
  163.   , RegisterClass("X")
  164.   , BaseClass("Y")
  165.   {
  166.     // some code to do with constructed static members of X
  167.     // make X usable to create instances
  168.   }
  169.   ...
  170. }
  171.  
  172. class Z : virtual X, virtual Y, virtual static RegisterClass
  173. {
  174.   static Z()
  175.   : X(), Y()
  176.   , RegisterClass("Z")
  177.   , BaseClass("X")
  178.   , BaseClass("Y")
  179.   {
  180.     // some code to do with constructed static members of Z
  181.     // make Z usable to create instances
  182.   }
  183.   ...
  184. }
  185.  
  186. ---
  187. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  188. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  189. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  190. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  191. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  192.